// router.js
import store from '@/store'
import {RouterMount,createRouter} from 'uni-simple-router'

//tabbar
const whitelist = {//声明了一个白名单
	'/pages/Index/Index': 'index',
	'/pages/Ballbar/Ballbar': 'ballbar',
	'/pages/Message/Message': 'message',
	'/pages/Match/MatchList': 'matchlist',
	'/pages/User/UserHome': 'userhome',
}
//统计页面访问记录
const visitStat = {
	//提交访问记录
	postVisitLog: (to, from) => {
		let visitList = sessionStorage.getItem('visit_page_log')
		if(visitList){
			visitList = JSON.parse(visitList)
		}else{
			visitList = []
		}
		if(to.meta.isTabbar === true){//打开tabbar页面
			sessionStorage.setItem('visit_page_log', JSON.stringify([]))
		}else{//打开其他页面
			if(to.fullPath != from.fullPath){//非直接打开
				let lastVisit = visitList[visitList.length - 1]
				if(lastVisit && lastVisit.fullPath == to.fullPath){//返回
					visitList.pop()
					sessionStorage.setItem('visit_page_log', JSON.stringify(visitList))
				}else{//非返回
					visitList.push(from)
					sessionStorage.setItem('visit_page_log', JSON.stringify(visitList))
					let fromName = from.meta.routeName
					let toName = to.meta.routeName
					//特殊处理
					if(to.name == 'articledetail'){
						if( (from.name == 'articledetail' && to.fullPath != from.fullPath) || from.name.includes('matchdetail') ){
							toName = '推荐方案详情'
						}
					}
					//提交数据
					if(fromName && toName){
						getApp().$CHS.saveVisitLog(fromName, toName)
					}
				}
			}else{//直接打开
				visitList = []
				sessionStorage.setItem('visit_page_log', JSON.stringify(visitList))
				if(to.meta.routeName){
					getApp().$CHS.saveVisitLog('直接打开', to.meta.routeName)
				}
			}
		}
	}
}

//创建路由router
const router = createRouter({
	platform: process.env.VUE_APP_PLATFORM,
	routes: [{
		aliasPath:'/',
		path: "/pages/Index/Index",
		name: 'index',
		meta: {
			isTabbar: true, //是否是tabbar页面
			routeName: '首页', //用于统计浏览数据
			noCheck: true //未登录允许访问
		}
	},
	{
		path: "/pages/Ballbar/Ballbar",
		aliasPath: "/ballbar",
		name: "ballbar",
		meta: {
			isTabbar: true, //是否是tabbar页面
			routeName: '球吧首页',
			noCheck: true //未登录允许访问
		}
	},
	{
		aliasPath:'/message',
		path: "/pages/Message/Message",
		name:'message',
		meta: {
			isTabbar: true, //是否是tabbar页面
			routeName: '消息首页'
		}
	},
	{
		aliasPath:'/matchlist',
		path: "/pages/Match/MatchList",
		name:'matchlist',
		meta: {
			isTabbar: true, //是否是tabbar页面
			routeName: '赛事首页',
			noCheck: true //未登录允许访问
		}
	},
	{
		aliasPath:'/userhome',
		path: "/pages/User/UserHome",
		name:'userhome',
		meta: {
			isTabbar: true ,//是否是tabbar页面
			noCheck: true //未登录允许访问
		}
	},{
		aliasPath:'/search',
		path: '/pages/Public/Search',
		name: 'search',
		meta: {
			noCheck: true //未登录允许访问
		}
	},{
		aliasPath:'/user/login/:random?',
		path: '/pages/Public/UserLogin',
		name: 'userlogin',
		meta: {
			noCheck: true, //未登录允许访问
			noRedirect: true, //登录后不回跳
			loginRedirect: true //登录后禁止访问
		}
	},{
		aliasPath: '/user/pwdforgot',
		path : "/pages/Public/ForgotPassword",
		name : "forgotpassword",
		meta: {
			noCheck: true, //未登录允许访问
			noRedirect: true, //登录后不回跳
			loginRedirect: true //登录后禁止访问
		}
	},{
		aliasPath:'/user/weixinlogin/:random?',
		path: "/pages/Public/WeixinLogin",
		name: "weixinlogin",
		meta: {
			noCheck: true, //未登录允许访问
			noRedirect: true, //登录后不回跳
			loginRedirect: true //登录后禁止访问
		}
	},{
		aliasPath:'/user/bindphone',
		path: '/pages/Public/BindPhone',
		name: 'bindphone',
		meta: {
			noCheck: true, //未登录允许访问
			loginRedirect: true //登录后禁止访问
		}
	},{
		aliasPath:'/user/resetlogin/:phone',
		path: "/pages/Public/ResetLogin",
		name: "resetlogin",
		meta: {
			noCheck: true, //未登录允许访问
			noRedirect: true, //登录后不回跳
			loginRedirect: true //登录后禁止访问
		}
	},{
		aliasPath:'/user/followseason',
		path: "/pages/Public/FollowSeason",
		name: "followseason",
	},{
		path: '/pages/Public/WebView',
		aliasPath: "/webview/:src",
		name: 'webview',
		meta: {
			noCheck: true //未登录允许访问
		},
	},{
		aliasPath:'/public/downlead/:random?',
		path: '/pages/Public/DownLead',
		name: 'downlead',
		meta: {
			noCheck: true //未登录允许访问
		},
		beforeEnter:(to,from,next)=>{
			if(!store.state.isLogin){
				let random = Date.now()
				next({name: 'downapp', params: {random}});
			}else{
				next();
			}
		}
	},{
		aliasPath:'/public/downapp/:random?',
		path: '/pages/Public/DownApp',
		name: 'downapp',
		meta: {
			noCheck: true //未登录允许访问
		},
		beforeEnter:(to,from,next)=>{
			if(to.query.down == 1){
				if(!store.state.isLogin){
					//保存回跳地址
					let redirectTo = {name: to.name, params: to.query}
					sessionStorage.setItem('redirectTo', JSON.stringify(redirectTo))
					if(to.query.inviteCode){//添加邀请码
						if(!to.query.inviteType){//兼容旧版本
							to.query.inviteType = to.query.inviteCode.length == 7 ? 'expert' : 'user'
						}
						let path = '/user/login/'+Date.now()+'?inviteType='+to.query.inviteType+'&inviteCode=' + to.query.inviteCode
						// next({path})
						if(store.state.weixin){
							next({name: 'weixinlogin', params: {random: Date.now()}})
						}else{
							next({path})
						}
					}else{
						// next({name: 'userlogin', params: {random: Date.now()}})
						if(store.state.weixin){
							next({name: 'weixinlogin', params: {random: Date.now()}})
						}else{
							next({name: 'userlogin', params: {random: Date.now()}})
						}
					}
				}else{
					next({name: 'downapp', params: {random: Date.now()}});
				}
			}else{
				next()
			}
		}
	},{
		aliasPath:'/article/reclist',
		path: '/pages/Article/RecommendList',
		name: 'reclist',
		meta: {
			noCheck: true //未登录允许访问
		}
	},{
		aliasPath: "/article/benefitslist",
		path : "/pages/Article/BenefitsList",
		name : "benefitslist",
	},{
		path: '/pages/Article/ArticleDetail',
		aliasPath: "/articledetail/:aid/:type?/:random?",
		name: 'articledetail',
		meta: {
			routeName: '方案详情',
			noCheck: true //未登录允许访问
		}
	},{
		path: '/pages/Public/VideoPlay',
		aliasPath: "/videoplay/:aid/:path",
		name: 'videoplay',

	},{
		path: "/pages/Article/ShareDetail",
		aliasPath: "/sharedetail/:did/:barId?",
		name: 'sharedetail',
		meta: {
			routeName: '晒单详情',
			noCheck: true //未登录允许访问
		}
	},{
		path: "/pages/Article/DiscussDetail",
		aliasPath: "/discussdetail/:did/:barId?",
		name: "discussdetail",
		meta: {
			routeName: '话题详情',
			noCheck: true //未登录允许访问
		},
		beforeEnter: (to, from, next) => {
			if(from.name == 'ballbar'){
				to.meta.routeName = '热帖详情'
			}else{
				to.meta.routeName = '话题详情'
			}
			next()
		}
	},{
		path: "/pages/Article/AddcontentDetail",
		aliasPath: "/addcontentdetail/:aid",
		name: 'addcontentdetail'
	},{
		path: "/pages/Article/SeasonArticle",
		aliasPath: "/seasonarticle/:eid/:sport/:sid",
		name: 'seasonarticle'
	},{
		path: "/pages/Article/VideoList",
		aliasPath: "/article/videolist/:type",
		name: 'videolist',
		meta: {
			noCheck: true //未登录允许访问
		},
	},{
		path: "/pages/Expert/ExpertList",
		aliasPath:'/experts',
		name:'experts',
		meta: {
			routeName: '专家',
			noCheck: true //未登录允许访问
		},
		beforeEnter: (to, from, next) => {
			if(to.query.title){
				console.log(to.query.title,'to.query.title')
				to.meta.routeName = to.query.title
			}else{
				to.meta.routeName = '专家'
			}
			next()
		}
	},{
		path: '/pages/Expert/SeasonRank',
		aliasPath: '/expert/seasonrank',
		name: 'seasonrank',
		meta: {
			routeName: '联赛排行榜',
			noCheck: true //未登录允许访问
		}
	},{
		path: '/pages/Expert/ExpertHome',
		aliasPath: '/experthome/:eid/:random?',
		name: 'experthome',
		meta: {
			routeName: '专家主页',
			noCheck: true //未登录允许访问
		}
	},{
		aliasPath:'/expert/search',
		path: "/pages/Expert/ExpertSearch",
		name: "expertsearch",
		meta: {
			noCheck: true //未登录允许访问
		}
	},{
		aliasPath:'/expert/cards/:eid/:random?',
		path: "/pages/Expert/ExpertCards",
		name: "expertcards",
	},{
		aliasPath:'/expert/numcard/:eid/:random?',
		path: "/pages/Expert/ExpertNumcard",
		name: "expertnumcard",
	},{
		aliasPath:'/expert/detail/:eid/:random?',
		path: "/pages/Expert/ExpertDetail",
		name: "expertdetail",
		meta: {
			noCheck: true //未登录允许访问
		}
	},{
		aliasPath: '/expert/reward/:eid/:random?',
		path: "/pages/Expert/ExpertReward",
		name: "expertreward",
		meta: {
			noCheck: true //未登录允许访问
		}
	},{
		aliasPath:'/user/couponlist',
		path: '/pages/User/CouponList',
		name: 'couponlist',
	},{
		aliasPath:'/user/coupon/:eid/:aid/:price',
		path: '/pages/Other/userCoupon',
		name: 'coupon',
	},{
		aliasPath:'/user/tradelist',
		path: '/pages/User/TradeList',
		name: 'tradelist',
	},{
		aliasPath:'/user/helpservice',
		path: '/pages/User/HelpService',
		name: 'helpservice',
		meta: {
			noCheck: true //未登录允许访问
		}
	},{
		aliasPath:'/user/orderlist',
		path: '/pages/User/OrderList',
		name: 'orderlist',
		meta: {
			routeName: '消费记录'
		}
	},{
		aliasPath:'/user/userset',
		path: '/pages/User/UserSet',
		name: 'userset',
	},{
		aliasPath:'/user/editnickname',
		path: "/pages/User/EditNickname",
		name: "editnickname",
	},{
		aliasPath:'/user/usersignin',
		path: '/pages/User/UserSignin',
		name: 'usersignin',
	},{
		aliasPath:'/user/userrecharge',
		path: '/pages/User/UserRecharge',
		name: 'userrecharge',
	},{
		aliasPath:'/user/userinfo',
		path: '/pages/User/UserInfo',
		name: 'userinfo',
	},{
		aliasPath:'/user/receivecoupon',
		path: "/pages/User/ReceiveCoupon",
		name: "receivecoupon",
	},{
		aliasPath:'/user/membercenter',
		path: '/pages/User/MemberCenter',
		name: 'membercenter',
	},{
		aliasPath:'/user/unlockarticle/:cardType/:cardId',
		path: '/pages/User/UnLockArticle',
		name: 'unlockarticle',
	},{
		aliasPath:'/user/readlog',
		path: '/pages/User/ReadLog',
		name: 'readlog',
		meta: {
			routeName: '浏览记录',
		}
	},{
		aliasPath:'/public/aboutus',
		path: '/pages/Public/AboutUs',
		name: 'aboutus',
	},{
		aliasPath:'/user/feedback',
		path: "/pages/User/FeedBack",
		name: "feedback",
		meta: {
			routeName: '投诉与建议'
		}
	},{
		aliasPath:'/user/selectexpert',
		path: "/pages/User/SelectExpert",
		name: "selectexpert",
	},{
		aliasPath:'/user/followexpert',
		path: "/pages/User/FollowExpert",
		name: "followexpert",
		meta: {
			routeName: '我的关注'
		}
	},{
		aliasPath:'/user/followmatch',
		path: "/pages/User/FollowMatch",
		name: "followmatch",
		meta: {
			routeName: '我的收藏'
		}
	},{
		aliasPath: '/user/completeinfo',
		path : "/pages/User/CompleteInfo",
		name : "completeinfo",
	},{
		aliasPath: '/user/comments',
		path: "/pages/User/UserComments",
		name: "usercomments",
	},{
		aliasPath: '/user/editpassword',
		path : "/pages/User/EditPassword",
		name : "editpassword",
	},{
		aliasPath: '/user/inviteuser',
		path : "/pages/User/InviteUser",
		name : "inviteuser",
		meta: {
			noCheck: true //未登录允许访问
		}
	},{
		aliasPath: '/user/inviteqrcode',
		path : "/pages/User/InviteQrcode",
		name : "inviteqrcode",
		meta: {
			noCheck: true //未登录允许访问
		}
	},{
		aliasPath: '/user/luckywheel',
		path : "/pages/User/LuckyWheel",
		name : "luckywheel",
		meta: {
			noCheck: true //未登录允许访问
		},
	},{
		aliasPath: '/user/luckyrecords',
		path : "/pages/User/LuckyRecords",
		name : "luckyrecords",
	},{
		aliasPath: '/user/growthreward',
		path : "/pages/User/GrowthReward",
		name : "growthreward",
	},{
		path: "/pages/Message/ChatBox",
		aliasPath: "/chatbox/:cid",
		name: "chatbox",
		meta: {
			routeName: '专家私信'
		}
	},{
		path: "/pages/Message/MessageSearch",
		aliasPath: '/message/search',
		name: "msgsearch",
	},{
		path: "/pages/Match/MatchDetailFoot",
		aliasPath: "/match/football/:mid",
		name: "matchfoot",
		meta: {
			routeName: '比赛详情',
			noCheck: true //未登录允许访问
		}
	},{
		path: "/pages/Match/MatchDetailBasket",
		aliasPath: "/match/basketball/:mid",
		name: "matchbasket",
		meta: {
			routeName: '比赛详情',
			noCheck: true //未登录允许访问
		}
	},{
		path: "/pages/Match/MatchZsFoot",
		aliasPath: "/match/football/zs/:mid",
		name: "matchzsfoot",
		meta: {
			noCheck: true //未登录允许访问
		}
	},{
		path: "/pages/Match/MatchZsBasket",
		aliasPath: "/match/basketball/zs/:mid",
		name: "matchzsbasket",
		meta:{
			noCheck: true //未登录允许访问
		}
	},{
		path: "/pages/Match/MatchSearch",
		aliasPath: "/match/search/:sportId/:day",
		name: "matchsearch",
		meta:{
			noCheck: true //未登录允许访问
		}
	},{
		path: "/pages/Match/MatchFilter",
		aliasPath: "/match/filter/:sportId/:type/:day",
		name: "matchfilter",
		meta:{
			noCheck: true //未登录允许访问
		}
	},{
		aliasPath:'/pay/resetpassword',
		path: '/pages/Pay/ResetPassword',
		name: 'resetpassword',
	},{
		aliasPath:'/pay/newpassword',
		path: '/pages/Pay/NewPassword',
		name: 'newpassword',
	},{
		aliasPath:'/pay/rechargesuccess',
		path: '/pages/Pay/RechargeSuccess',
		name: 'rechargesuccess',
	},{
		path: '/pages/Pay/WxPay',
		aliasPath:'/pay/wxpay',
		name: 'wxpay',
		meta: {
			noCheck: true, //未登录允许访问
			noRedirect: true, //登录后不回跳
		},
		beforeEnter:(to,from,next)=>{
			if(to.query.wxcode) {
				console.log(to.query.wxcode,'to.query.wxcode')
				next();
			}else{
				let haveOrder = uni.getStorageSync('haveOrder')
				if(haveOrder){
					if(to.query.wxback){
						next({name:'userrecharge', params:{back: 1, success: 1}, NAVTYPE: 'replace'});
					}else{
						next({name:'userrecharge', NAVTYPE: 'replace'});
					}
				}else{
					if(Object.keys(to.query).length == 0){
						uni.removeStorageSync('orderModel')
						next({name:'userrecharge', NAVTYPE: 'replace' });
					}else{
						next();
					}
				}
			}
		}
	},{
		path: '/pages/Pay/AliPay',
		aliasPath:'/pay/alipay',
		name: 'alipay',
		meta: {
			noCheck: true, //未登录允许访问
			noRedirect: true, //登录后不回跳
		},
		beforeEnter:(to,from,next)=>{
			let isFromWx = uni.getStorageSync('isFromWx')
			if(isFromWx || to.query.isFromWx) {
				next();
			}else{
				let haveOrder = uni.getStorageSync('haveOrder')
				if(haveOrder){
					console.log(haveOrder,'haveOrder---')
					next({name:'userrecharge', params:{ back: 1, success: 1 }, NAVTYPE: 'replace' });
				}else{
					if(Object.keys(to.query).length == 0){
						uni.removeStorageSync('orderModel')
						next({name:'userrecharge', NAVTYPE: 'replace' });
					}else{
						next();
					}
				}
			}
		}
	},
	{
		path: '/pages/Pay/zfbPay',
		aliasPath:'/pay/zfbpay',
		name: 'zfbpay',
		meta: {
			noCheck: true, //未登录允许访问
			noRedirect: true, //登录后不回跳
		},
		beforeEnter:(to,from,next)=>{
			let aid=uni.getStorageSync('aid')
			let isFromWx = uni.getStorageSync('zfbisFromWx')
			if(isFromWx || to.query.isFromWx) {
				next();
			}else{
				let haveOrder = uni.getStorageSync('zfbhaveOrder')
				if(haveOrder){
					console.log(haveOrder,'haveOrder---')
					next({name:'articledetail', params:{aid: aid, random: Date.now(),back: 1, success: 1},NAVTYPE: 'replace'});
				}else{
					if(Object.keys(to.query).length == 0){
						uni.removeStorageSync('zfborderModel')
						next({name:'articledetail',params:{aid: aid, random: Date.now()},NAVTYPE: 'replace'});
					}else{
						next();
					}
				}
			}
		}
	},{
		path: '/pages/Pay/weixPay',
		aliasPath:'/pay/weixpay',
		name: 'weixpay',
		meta: {
			noCheck: true, //未登录允许访问
			noRedirect: true, //登录后不回跳
		},
		beforeEnter:(to,from,next)=>{
			let aid=uni.getStorageSync('aid')
			if(to.query.wxcode) {
				console.log(to.query.wxcode,'to.query.wxcode')
				next();
			}else{
				let haveOrder = uni.getStorageSync('zfbhaveOrder')
				if(haveOrder){
					if(to.query.wxback){
						next({name:'articledetail', params:{aid: aid, random: Date.now(),back: 1, success: 1},NAVTYPE: 'replace'});
					}else{
						next({name:'articledetail',params:{aid: aid, random: Date.now()},NAVTYPE: 'replace'});
					}
				}else{
					if(Object.keys(to.query).length == 0){
						uni.removeStorageSync('zfborderModel')
						next({name:'articledetail',params:{aid: aid, random: Date.now()},NAVTYPE: 'replace'});
					}else{
						next();
					}
				}
			}
		}
	},{
		path: "/pages/User/Apply/ApplyExpert",
		aliasPath: "/user/apply/expert",
		name: "applyexpert"
	},{
		path: "/pages/User/Apply/SubmitUserinfo",
		aliasPath: "/user/apply/submitinfo",
		name: "applyinfo",
		beforeEnter:(to,from,next)=>{
			console.log(from);
			if(from.name != '' && from.name != 'applyexpert' ){
				next({name: 'userhome', NAVTYPE: 'pushTab'})
			}else{
				next()
			}
		}
	},{
		path: "/pages/User/Apply/SubmitArticle",
		aliasPath: "/user/apply/submitarticle",
		name: "applyarticle",
		beforeEnter:(to,from,next)=>{
			console.log(from);
			if(from.name != 'applyinfo' && from.name != 'applypublish'){
				next({name: 'userhome', NAVTYPE: 'pushTab'})
			}else{
				next()
			}
		}
	},{
		path: "/pages/User/Apply/PublishArticle",
		aliasPath: "/user/apply/publish",
		name: "applypublish",
		beforeEnter:(to,from,next)=>{
			console.log(from);
			if(from.name != 'applyarticle' && from.name != 'selectmatch'){
				next({name: 'userhome', NAVTYPE: 'pushTab'})
			}else{
				next()
			}
		}
	},{
		path: "/pages/User/Apply/SelectMatch",
		aliasPath: "/user/apply/selectmatch",
		name: "selectmatch",
		beforeEnter:(to,from,next)=>{
			console.log(from);
			if(from.name != 'applypublish'){
				next({name: 'userhome', NAVTYPE: 'pushTab'})
			}else{
				next()
			}
		}
	},{
		path: "/pages/User/Apply/ApplyStatus",
		aliasPath: "/user/apply/status",
		name: "applystatus"
	},{
		path: "/pages/User/UserArticle",
		aliasPath: "/user/userarticle",
		name: "userarticle",
		meta: {
			routeName: '我的发布'
		}
	},{
		path: "/pages/User/UserRenown",
		aliasPath: "/user/userrenown",
		name: "userrenown"
	},{
		path: "/pages/User/RenownList",
		aliasPath: "/user/renownlist",
		name: "renownlist"
	},{
		path: "/pages/Ballbar/BallbarHome",
		aliasPath: "/ballbar/home/:bid",
		name: "barhome",
		meta: {
			routeName: '球吧主页',
			noCheck: true, //未登录允许访问
		}
	},{
		path: "/pages/Ballbar/BallbarSet",
		aliasPath: "/ballbar/set/:bid",
		name: "barset",
		meta: {
			routeName: '球吧设置'
		}
	},{
		path: "/pages/Ballbar/BallbarList",
		aliasPath: "/ballbar/list",
		name: "barlist",
		meta: {
			routeName: '球吧列表页'
		}
	},{
		path: "/pages/Ballbar/BallbarMember",
		aliasPath: "/ballbar/member/:bid",
		name: "barmember",
		meta: {
			routeName: '球吧成员列表'
		}
	},{
		path: "/pages/Ballbar/MemberHome",
		aliasPath: "/ballbar/memberhome/:userId",
		name: "memberhome",
		meta: {
			routeName: '用户主页'
		}
	},{
		path: "/pages/Ballbar/BallbarSearch",
		aliasPath: "/ballbar/search/:type/:bid?",
		name: "barsearch",
		meta:{
			noCheck: true, //未登录允许访问
		}
	},{
		path: "/pages/Ballbar/BallbarNotice",
		aliasPath: "/ballbar/notice",
		name: "barnotice"
	},{
		path: "/pages/Ballbar/BallbarAuth",
		aliasPath: "/ballbar/userauth",
		name: "userauth",
		beforeEnter: (to,from,next) => {
			if(from.name != 'barhome'){
				next({name: 'ballbar', NAVTYPE: 'pushTab'})
			}else{
				next()
			}
		}
	},{
		path: "/pages/Ballbar/Publish/ContentEditor",
		aliasPath: "/ballbar/publish/editor/:type",
		name: "contenteditor",
		beforeEnter: (to,from,next) => {
			console.log(from);
			if(from.name.indexOf('publish') < 0){
				next({name: 'ballbar', NAVTYPE: 'pushTab'})
			}else{
				next()
			}
		}
	},{
		path: "/pages/Ballbar/Publish/ArticlePublish",
		aliasPath: "/ballbar/publish/article/:bid",
		name: "articlepublish",
		beforeEnter: (to,from,next) => {
			if(from.name == 'articlepublish'){
				next({name: 'ballbar', NAVTYPE: 'pushTab'})
			}else{
				next()
			}
		}
	},{
		path: "/pages/Ballbar/Publish/DiscussPublish",
		aliasPath: "/ballbar/publish/discuss/:bid",
		name: "discusspublish",
		beforeEnter: (to,from,next) => {
			if(from.name == 'discusspublish'){
				next({name: 'ballbar', NAVTYPE: 'pushTab'})
			}else{
				next()
			}
		}
	},{
		path: "/pages/Ballbar/Publish/SelectMatch",
		aliasPath: "/ballbar/publish/selectmatch",
		name: "publishselectmatch",
		beforeEnter: (to,from,next) => {
			console.log(from);
			if(from.name != 'articlepublish'){
				next({name: 'ballbar', NAVTYPE: 'pushTab'})
			}else{
				next()
			}
		}
	},{
		path: "/pages/Other/ExpertRules",
		aliasPath: "/other/expertrules",
		name: "expertrules",
		meta: {
			noCheck: true //未登录允许访问
		}
	},{
		path: "/pages/Other/Agreement",
		aliasPath: "/agreement/:name",
		name: "agreement",
		meta: {
			noCheck: true //未登录允许访问
		}
	},{
		path: "/pages/Other/Question",
		aliasPath: "/other/question",
		name: "question",
		meta: {
			noCheck: true //未登录允许访问
		}
	},{
		path: "*",
		name: 'moddle',
		redirect: (to) => {
			console.log(to);
			return {name:'index'}
		}
	}],
	routerErrorEach:(error, router)=>{
		// 跳转时错误后执行的守卫
		console.log(error,router);
		if(error.type !== 0){
			uni.showToast({
				title: '页面打开失败，请刷新浏览器',
				icon: 'none'
			})
		}
		const pattern = /Loading chunk (\d)+ failed/g;
		const isChunkLoadFailed = error.message.match(pattern);
		if (isChunkLoadFailed) {
		  window.location.reload();
		  // router.replace(targetPath);
		} else {
		  console.log('no')
		}
	},
});
//全局路由前置守卫
router.beforeEach((to, from, next) => {
	console.log('跳转开始to', to);
	if(to.query.inviteCode){//邀请码保存
		sessionStorage.setItem('inviteCode', to.query.inviteCode)
	}
	if(to.query.inviteType){//邀请码类型
		sessionStorage.setItem('inviteType', to.query.inviteType)
	}
	if(to.query.specialCode){//特殊邀请码保存
		sessionStorage.setItem('specialCode', to.query.specialCode)
	}
	if(store.state.isLogin){//登录后允许访问
		if(to.meta.loginRedirect === true){//登录后禁止访问页面
			next({name: 'index'})
			//next({name: 'downlead', params: {random: Date.now()}});
		}else{//其他页面自由访问
			next()
		}
	} else if ( to.meta.noCheck === true ){//未登录允许访问页面
		if(to.query.inviteType == 'user'){
			//用户邀请重置回跳页面
			sessionStorage.setItem('redirectTo', JSON.stringify({name: 'downlead', params: {random: Date.now()}}))
		}
		next()
	} else if ( to.meta.noRedirect === true ){//登录后不回跳页面
		next({name: 'userlogin', params: {random: Date.now()}})
	} else {//未登录跳转登录页面
		//保存回跳地址
		let redirectTo = {name: to.name, params: to.query}
		console.log('next',redirectTo)
		sessionStorage.setItem('redirectTo', JSON.stringify(redirectTo))
		if(to.query.inviteCode){//添加邀请码
			if(!to.query.inviteType){//兼容旧版本
				to.query.inviteType = to.query.inviteCode.length == 7 ? 'expert' : 'user'
			}
			let path = '/user/login/'+Date.now()+'?inviteType='+to.query.inviteType+'&inviteCode=' + to.query.inviteCode
			if(store.state.weixin){
				next({name: 'weixinlogin', params: {random: Date.now()}})
			}else{
				next({path})
			}
		}else{
			// next({name: 'userlogin', params: {random: Date.now()}})
			console.log('next')
			if(store.state.weixin){
				next({name: 'weixinlogin', params: {random: Date.now()}})
			}else{
				next({name: 'userlogin', params: {random: Date.now()}})
			}
		}
	}
});
//全局路由后置守卫
router.afterEach((to, from) => {
	console.log('跳转结束', from)
	if(to.meta.isTabbar === true){
		if(store.state.newMessage > 0){
			let textNum = store.state.newMessage > 99 ? '99+' : store.state.newMessage.toString()
			uni.setTabBarBadge({
				index: 1,
				text: textNum
			})
		}else if(store.state.newMessage == 0){
			uni.removeTabBarBadge({
				index: 1
			})
		}
	}
	//提交访问日志
	visitStat.postVisitLog(to, from)
});
export {
	router,
	RouterMount
}